package com.example.stumanager.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.stumanager.entity.LateReturnEntity;

import java.util.ArrayList;
/**
 * 晚归打卡数据库操作类
 */
public class LateReturnSql extends SQLiteOpenHelper {

     public LateReturnSql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }
        private SQLiteDatabase dp;
        public LateReturnSql(Context context){
            super(context,"ClockInLate",null,1);
            dp=this.getWritableDatabase();
        }
        public boolean add(LateReturnEntity lateReturnEntity){
            ContentValues values=new ContentValues();
            values.put("number", lateReturnEntity.getNumber());
            values.put("name", lateReturnEntity.getName());
            values.put("explains", lateReturnEntity.getExplains());
            values.put("face", lateReturnEntity.getFace());
            values.put("state", lateReturnEntity.getState());
            long i=dp.insert("clock",null,values);

            if(i>0){
                Log.d("","插入成功");
                return true;
            }
            return false;
        }


        public ArrayList getAll(){
            ArrayList array=new ArrayList();
            Cursor cursor=dp.query("clock",null,null,null,null,null,null);
            while(cursor.moveToNext()){
                @SuppressLint("Range") String number=cursor.getString( cursor.getColumnIndex("number"));
                @SuppressLint("Range") String name=cursor.getString( cursor.getColumnIndex("name"));
                @SuppressLint("Range") String explains=cursor.getString( cursor.getColumnIndex("explains"));
                @SuppressLint("Range") String state=cursor.getString( cursor.getColumnIndex("state"));
                @SuppressLint("Range") String face=cursor.getString(cursor.getColumnIndex("face"));
                LateReturnEntity u=new LateReturnEntity(number,name,explains,state,face);
                array.add(u);
            }
            return array;
        }



        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            String sql="create table clock(number text not null,name text not null,explains text not null,state text not null,face text)";
            sqLiteDatabase.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }

}
